<template>
	<scroll-view class="prolist" scroll-y>
		<view class="status_bar">
			<view class="top_view"></view>
		</view>
		<view class="content">
			<view class="searchbox">
				<!-- <input type="text" class="s_input" placeholder="搜索" v-model="searchtxt" return-key-type="search" @blur="search" /> -->
				品潮君
			</view>
			<scroll-view class="scroller" scroll-x>
				<view class="auto">
					<text @click="changeCity(-1)" class="scrollItem" :class="curCity ==-1?'curItem':''">推荐</text>
					<text @click="changeCity(inx,city.city_id)" class="scrollItem" :class="curCity ==inx?'curItem':''" v-for="(city,inx) in citys"
					 :key="city.city_id"> {{city.city_name}} </text>
				</view>
				
			</scroll-view>
			<view class="moredata">
				<proitem :item="itm" v-for="(itm,inx) in list" :key="inx" @goInfo="goInfo(itm.id)" @follow="follow(itm)"></proitem>
				<view v-if="list.length==0" class="nomoredata"><text class="nomoredtxt">无更多数据</text></view>
			</view>
		</view>
	</scroll-view>
</template>

<script>
	import proitem from "@/components/pariseItem.vue";
	export default {
		components: {
			proitem
		},
		onPullDownRefresh() {
			console.log('refresh');
			this.list = [];
			this.curPage =1;
				this.getData(this.istuijian, this.cityId);
		},
		data() {
			return {
				searchtxt: "", //搜索文字
				curCity: -1,
				istuijian: "recommend",
				cityId: 0,
				curPage: 1,
				sys: {},
				list: [],
				loadinging: true,
				userInfo: {},
				account: {},
				citys: []
			}
		},

		onShow() {
			this.list =[];
			var _this = this;
			uni.getStorage({
				key: "$system",
				success(res) {
					_this.sys = res.data;
					console.log("system: " + JSON.stringify(res.data));
				}
			});

			//let result = new Promise()
 
			const value = uni.getStorageSync('storage_key');
			console.log("value : " + JSON.stringify( value ));
			if (value.uid) {
				_this.userInfo = value;
				 _this.getData(this.istuijian, this.cityId);
				if (_this.userInfo.ispass) {
					uni.showToast({
						title: '账号已被查封，请与管理员联系',
						icon: "none"
					});
					uni.navigateTo({
						url: '/pages/public/login/login'
					});
				}
			} else {
				_this.getData();
				_this.getcitys();
			}
			

			/*  */
			/* uni.getStorage({
				key:"citys",
				success: (e) => {
					if(new Date() - new Date(e.data.time)<1000 *60*60*24){
						console.log("time is not over",e.data.time)
						this.citys = e.data.citys;
					}
					else{
						this.getcitys();
					}
				}
			}) */
		},
		methods: {
			loadmoreasd() {
				console.log("loadmore");
			},
			follow(itm) {
				let url = "";
				let _this = this;
				console.log("follow:",JSON.stringify( _this.userInfo))
				if (!_this.userInfo.uid) {
					uni.showToast({
						icon: "none",
						title: "未登录！"
					})
					uni.navigateTo({
						url: '/pages/public/login/login'
					});
					return;
				}
				if (itm.isFollow == 0) {
					url = "http://apptest.pinchao.cc/api.php/Store/follow";
				} else {
					url = "http://apptest.pinchao.cc/api.php/Store/delFollow";
				}
				console.log(url)
				uni.request({
					url: url,
					method: "post",
					header: {
						app_version: '1.0.0',
						system_version: _this.sys.system,
						"Content-Type": "application/x-www-form-urlencoded"
					},
					data: {
						"store_id": itm.id,
						"access_token": _this.userInfo.access_token,
						"uid": _this.userInfo.uid
					},

					success(res) {
						//console.log(res)
						if (res.data.ret == '200') {
							console.log(res.data.msg, _this.userInfo.uid);
							if (itm.isFollow == 0) {
								itm.isFollow = 1;
								uni.showToast({
									title: "关注成功"
								})
							} else {
								itm.isFollow = 0;
								uni.showToast({
									title: "取消关注成功"
								})
							}

						} else {
							uni.showToast({
								icon: "none",
								title: res.data.msg
							})
						}

					},
					error(err) {
						console.log("err:", err)
					}
				})
			},
			goInfo(itm) {
				console.log("itm:", itm)
				uni.setStorage({
					key: "proId",
					data: itm
				})
				uni.navigateTo({
					url: "/pages/home/proInfo?id=" + itm.id + "&token=" + itm.access_token + "&uid=" + itm.uid
				})

			},
			loadMoreData() {
				this.curPage++;
				this.getData();
			},
			onloading() {},
			search(e) {
				this.list = [];
				console.log("cityid:",this.cityId);
				//this.getData(this.istuijian, this.cityId);
			},
			go_info(e) {},
			changeCity(inx, cityId) {
				this.curCity = inx;
				this.curPage = 1;
				this.list = [];
				if (inx == -1) {
					this.getData();
					this.istuijian = "recommend";
					this.cityId = 0;
				} else {

					this.istuijian = "city";
					this.cityId = cityId;
					this.getData("city", cityId);
				}
			},
			getcitys() {
				var _this = this;
				uni.request({
					url: "http://apptest.pinchao.cc/api.php/City/getList",
					method: "post",
					header: {
						app_version: '1.0.0',
						system_version: _this.sys.system,
						"Content-Type": "application/x-www-form-urlencoded"
					},

					dataType: "json",
					success(res) {
						console.log(res)
						if (res.data.ret == '200') {
							_this.citys = res.data.data;
							let nowtime = new Date();
							//uni.setStorage("key":"citys","data":{"citys":_this.citys,"time":nowtime})
							uni.setStorage({
								key: "citys",
								data: {
									"citys": _this.citys,
									"time": nowtime
								}
							})
						} else {
							uni.showToast({
								icon: "none",
								title: "ss"
							})
						}

					},
					error(err) {
						console.log("err:", err)
					}
				})
			},
			getData(str = "recommend", cityId = "-1") {
				var _this = this;
				console.log("_this.userInfo:", _this.userInfo);
				uni.request({
					url: "http://apptest.pinchao.cc/api.php/Store/getList",
					method: "post",
					header: {
						app_version: '1.0.0',
						system_version: _this.sys.system,
						"Content-Type": "application/x-www-form-urlencoded"
					},
					data: {
						"access_token": _this.userInfo.access_token,
						"uid": _this.userInfo.uid,
						"type": str,
						"keyword": this.searchtxt,
						"city_id": cityId,
						"page": this.curPage
					},

					success(res) {
						console.log(res.data)
						if (res.data.ret == '200') {
							_this.list = _this.list.concat(res.data.data);
						} else {
							uni.showToast({
								icon: "none",
								title: res.data.msg
							})
						}

					},
					error(err) {
						console.log("err:", err)
					},
					complete() {
						
						uni.stopPullDownRefresh();
						_this.loadinging = false;
					}
				})
			},
		}
	}
</script>

<style>
	.moredata{
		margin-top: 158upx;
	}
	.nomoredata{
		width:100%;
		height:60rpx;
		line-height: 60upx;
		margin-top:40rpx;
		margin:0 auto;
		text-align:center;
	
		
	}
	
	.nomoredtxt{
		text-align: center;
		color: #A0A0A0;
	}
	.auto{
		min-width: 1800upx;
		height: auto;
	}
	.listsaa {
		flex-direction: row;
	}

	.cellasd {
		flex-direction: row;
	}

	.prolist {
		width: 100%;
	}

	.status_bar {
		width: 100%;
		height: 44upx;
		background-color: #000;
		
	}

	.searchbox {
		width: 100%;
		height: 84upx;
		margin-top: -2upx;
		background-color: #000;
		text-align: center;
		line-height: 84upx;
		color: #fff;
		font-size: 36upx;
		position: fixed;
		top: 44upx;
		
	}

	.s_input {
		width: 702;
		height: 60upx;
		margin: 12upx 24upx;
		background-color: #fff;
		text-align: center;
	}

	.scroller {
		margin-top: ;
		width: 750upx;
		height: 74upx;
		border-bottom-width: 1upx;
		border-bottom-style: solid;
		border-bottom-color: #E6E6E6;
		overflow-y: auto;
		position: fixed;
		top: 128upx;
		background-color: #fff;
	}

	.scrollItem {
		width: 80upx;
		height: 74upx;
		margin: 0 20upx;
		text-align: center;
		line-height: 74upx;
		display: inline-block;
		font-size: 32upx;
	}

	.curItem {
		border-bottom-width: 4upx;
		border-bottom-color: #DBC600;
		color: #E1CE29;
	}
</style>
